package mspdt.ontrade;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

public class ViewProduct extends Activity{
	
	ListView lv_prod = null;
	DBAdapter db;
	LinearLayout background = null;
	int product_id = 0;
	ArrayList<String> names = new ArrayList<String>();
	ArrayList<String> ingredients = new ArrayList<String>();
	ArrayList<Integer> r_images = new ArrayList<Integer>();
	ArrayList<Integer> l_images = new ArrayList<Integer>();
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.view_product);
		lv_prod = (ListView)findViewById(R.id.list_product);
		Bundle bundle = getIntent().getExtras();
		product_id = bundle.getInt("ID");
		background = (LinearLayout)findViewById(R.id.prod_back);
		db = new DBAdapter(this);
		db.openDataBase();
		names = db.getNames(product_id);
		ingredients = db.getIngredients(product_id);
		l_images = getImages(product_id, names.size());
		
		ProductAdapter adapter = new ProductAdapter(this, names, ingredients, l_images, r_images);
		db.close();
		lv_prod.setAdapter(adapter);
		lv_prod.setCacheColorHint(Color.WHITE);
		lv_prod.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				Bundle bundle = new Bundle();
				
				Intent product = new Intent(getApplicationContext(), ProductInfo.class);
				
				bundle.putInt("IMAGE",  l_images.get(arg2));
				bundle.putString("NAME", names.get(arg2));
				bundle.putString("INFO", ingredients.get(arg2));
				bundle.putInt("BACKGROUND", product_id);
				
				product.putExtras(bundle);
				startActivity(product);
			}
		});
	}
	public ArrayList<Integer> getImages(int ID, int size)
	{
		//int default_pic = R.drawable.noimage;
		ArrayList<Integer> image = new ArrayList<Integer>();
		for(int i=0;i<size;i++)
		{
			image.add(R.drawable.noimage);
		}
		switch(ID)
		{
		case 2: 
			int[] co_01 = {55,56,5,6,8,18,41,32};
			int[] co_02 = {0,34,16,35,36,22,37,24,38,31,47,48,50,51};
			int[] co_03 = {7};
			int[] co_04 = {9,12,13,19,21,26,28,40,42,45,49,52,53,54};
			int[] co_05 = {10,14,23,27,29,44,15};
			int[] co_06 = {11};
			int[] co_07 = {17};
			int[] co_08 = {20,46};
			int[] co_09 = {25};
			int[] co_10 = {39};
			int[] co_11 = {30};
			int[] co_12 = {43};
			for(int i=0;i<co_01.length;i++)
			{
				image.set(co_01[i], R.drawable.co_01);
			}
			for(int i=0;i<co_02.length;i++)
			{
				image.set(co_02[i], R.drawable.co_02);
			}
			for(int i=0;i<co_03.length;i++)
			{
				image.set(co_03[i], R.drawable.co_03);
			}
			for(int i=0;i<co_04.length;i++)
			{
				image.set(co_04[i], R.drawable.co_04);
			}
			for(int i=0;i<co_05.length;i++)
			{
				image.set(co_05[i], R.drawable.co_05);
			}
			for(int i=0;i<co_06.length;i++)
			{
				image.set(co_06[i], R.drawable.co_06);
			}
			for(int i=0;i<co_07.length;i++)
			{
				image.set(co_07[i], R.drawable.co_07);
			}
			for(int i=0;i<co_08.length;i++)
			{
				image.set(co_08[i], R.drawable.co_08);
			}
			for(int i=0;i<co_09.length;i++)
			{
				image.set(co_09[i], R.drawable.co_09);
			}
			for(int i=0;i<co_10.length;i++)
			{
				image.set(co_10[i], R.drawable.co_10);
			}
			for(int i=0;i<co_11.length;i++)
			{
				image.set(co_11[i], R.drawable.co_11);
			}
			for(int i=0;i<co_12.length;i++)
			{
				image.set(co_12[i], R.drawable.co_12);
			}
		//background.setBackgroundResource(R.drawable.flip2);
		break;
		case 4: image.set(2, R.drawable.prod_juice1); break;
		
		case 5: 
			image.set(0, R.drawable.prod_vodka1);
			image.set(1, R.drawable.prod_vodka2);
			image.set(2, R.drawable.prod_vodka5);
			image.set(3, R.drawable.prod_vodka4);
			image.set(4, R.drawable.prod_vodka7);
		break;
		default: break;
		}
		return image;
	}

}
